//
//  XMPublishView.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/6/5.
//

#import "XMPublishView.h"
#import "XMTopicView.h"
#import "XMDateSelectView.h"
#import "XMPubPriceView.h"
#import "XMRequireView.h"
#import "XMServiceModel.h"
#import "UIButton+WebCache.h"

@interface XMPublishView ()<UITextViewDelegate, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>

//@property (nonatomic, strong) UIScrollView *bodyScrollView;
@property (nonatomic, strong) UITableView *tbl;
@property (nonatomic, strong) UITextField *titleTextField;
@property (nonatomic, strong) UITextView *contentTextView;
@property (nonatomic, strong) UILabel *placeholderLabel;
@property (nonatomic, strong) UIScrollView *imgBgView;
@property (nonatomic, strong) UIButton *addImgBtn;

@property (nonatomic, strong) UIScrollView *topicBgView;

@property (nonatomic, strong) XMTopicView *topicView;

@property (nonatomic, strong) XMDateSelectView *dateSelectView;  //日期选择
@property (nonatomic, strong) XMPubPriceView *priceView;   //价格
@property (nonatomic, strong) XMRequireView *requireView;  //要求
@property (nonatomic, strong) UIScrollView *requireSelectBgView;

@property (nonatomic, strong) NSArray *serviceModelArr;  //见面类型
@property (nonatomic, strong) NSMutableArray *itemBtnArr;   //见面类型按钮
@property (nonatomic, strong) NSMutableArray *selectTopicArr;  //添加的话题
@property (nonatomic, strong) NSMutableArray *selectImgArr;  //上传的图片
 
@property (nonatomic, strong) NSMutableDictionary *publishDic;  //需要提交的数据

@property (nonatomic, copy) NSString *positionStr;
@property (nonatomic, copy) NSString *startTimeStr;
@property (nonatomic, copy) NSString *endTimeStr;
@property (nonatomic, copy) NSString *priceStr;

@end

@implementation XMPublishView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.navView];
    self.navView.backgroundColor = kColorHEX(0xffffff);
    _positionStr = @"";//@"乐凯大街12号";
    _startTimeStr = @"";//@"2025.03.22 11:00";
    _endTimeStr = @"";//@"2025.03.30 12:00";
    _priceStr = @"";//@"¥99.00";
    
    UIButton *publishBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(14.0) frame:CGRectMake(self.width - KScaleWidth(8.0) - KScaleWidth(55.0), kNavagationBarH - 44 + (44 - KScaleWidth(24.0))/2, KScaleWidth(55.0), KScaleWidth(24.0)) title:@"发布" titleColor:kColorHEX(0x333333)];
    [publishBtn setRadius:publishBtn.height/2 borderColor:kColorHEX(0x979797) borderWidth:1];
    [publishBtn addTarget:self action:@selector(publishAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.navView addSubview:publishBtn];
    
    UIButton *draftBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(14.0) frame:CGRectMake(publishBtn.x - KScaleWidth(5.0) - KScaleWidth(55.0), publishBtn.y, KScaleWidth(55.0), publishBtn.height) title:@"草稿" titleColor:kColorHEX(0x333333)];
    [draftBtn setRadius:draftBtn.height/2 borderColor:kColorHEX(0x979797) borderWidth:1];
    [draftBtn addTarget:self action:@selector(draftAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.navView addSubview:draftBtn];
    
    
    //主页面
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(680.0))];
    headView.backgroundColor = kColorHEX(0xffffff);
    [self addSubview:self.tbl];
    _tbl.tableHeaderView = headView;
    
//    NSArray *titleArr = @[@"见一面", @"下午茶", @"Citywalk", @"吃顿饭", @"我的主场", @"Go shopping", @"看电影", @"夜生活", @"养生局"];
//    NSArray *itemIconArr = @[@"pub_meeting_icon", @"pub_afternoon_tea_icon", @"pub_citywalk_icon", @"pub_meal_icon", @"pub_home_court_icon", @"pub_shop_icon", @"pub_film_icon", @"pub_night_life_icon", @"pub_health_icon"];
    CGFloat itemWidth = (self.width - KScaleWidth(8.0)*4)/3;
    for (int i = 0; i < _serviceModelArr.count; i ++) {
        XMServiceModel *serviceModel = _serviceModelArr[i];
//        NSString *itemIconStr = itemIconArr[i];
        UIButton *itemBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(15.0) frame:CGRectMake(KScaleWidth(8.0) + (itemWidth + KScaleWidth(8.0))*(i%3), KScaleWidth(15.0) + (KScaleWidth(90.0) + KScaleWidth(8.0))*(i/3), itemWidth, KScaleWidth(90.0)) title:serviceModel.name titleColor:kColorHEX(0x999999)];
        [itemBtn setTitleColor:kColorHEX(0x333333) forState:UIControlStateSelected];
        [itemBtn setTitleEdgeInsets:UIEdgeInsetsMake(KScaleWidth(20.0), 0, -KScaleWidth(20.0), 0)];
        [itemBtn setBackgroundImage:[UIImage imageWithColor:kColorHEX(0xF6F7F9)] forState:UIControlStateNormal];
        [itemBtn setBackgroundImage:[UIImage imageWithColor:kColorHEX(0xFEFDF7)] forState:UIControlStateSelected];
        [itemBtn setRadius:KScaleWidth(16.0) borderColor:kColorHEX(0xF6F7F9) borderWidth:1];
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake((itemBtn.width - KScaleWidth(38.0))/2, KScaleWidth(13.0), KScaleWidth(38.0), KScaleWidth(38.0))];
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        imgView.tag = 32;
        [imgView sd_setImageWithURL:[NSURL URLWithString:serviceModel.icon]];
        [itemBtn addSubview:imgView];
        [itemBtn addTarget:self action:@selector(itemSelectAction:) forControlEvents:UIControlEventTouchUpInside];
//        [itemBtn layoutButtonWithEdgeInsetsStyle:CSButtonEdgeInsetsStyleTop ImageTitleSpace:KScaleWidth(5.0)];
        itemBtn.tag = 9000 + [serviceModel.objectId integerValue];
        [headView addSubview:itemBtn];
        [self.itemBtnArr addObject:itemBtn];
        if ([serviceModel.name isEqualToString:@"我的主场"]) {
            itemBtn.selected = YES;
            [imgView sd_setImageWithURL:[NSURL URLWithString:serviceModel.activeIcon]];
        }
    }
//    for (int i = 0; i < 9; i ++) {
//        NSString *itemIconStr = itemIconArr[i];
//        UIButton *itemBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(KScaleWidth(8.0) + (itemWidth + KScaleWidth(8.0))*(i%3), KScaleWidth(15.0) + (KScaleWidth(90.0) + KScaleWidth(8.0))*(i/3), itemWidth, KScaleWidth(90.0)) image:itemIconStr selectImg:[NSString stringWithFormat:@"%@_select", itemIconStr]];
//        [itemBtn setTitle:titleArr[i] forState:UIControlStateNormal];
//        [itemBtn setTitleColor:kColorHEX(0x999999) forState:UIControlStateNormal];
//        [itemBtn setTitleColor:kColorHEX(0x333333) forState:UIControlStateSelected];
//        [itemBtn setBackgroundImage:[UIImage imageWithColor:kColorHEX(0xF6F7F9)] forState:UIControlStateNormal];
//        [itemBtn setBackgroundImage:[UIImage imageWithColor:kColorHEX(0xFEFDF7)] forState:UIControlStateSelected];
//        [itemBtn setRadius:KScaleWidth(16.0) borderColor:kColorHEX(0xF6F7F9) borderWidth:1];
//        [itemBtn addTarget:self action:@selector(itemSelectAction:) forControlEvents:UIControlEventTouchUpInside];
//        [itemBtn layoutButtonWithEdgeInsetsStyle:CSButtonEdgeInsetsStyleTop ImageTitleSpace:KScaleWidth(5.0)];
//        [headView addSubview:itemBtn];
//        [self.itemBtnArr addObject:itemBtn];
//    }
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, (KScaleWidth(90.0) + KScaleWidth(8.0))*3 + KScaleWidth(20.0), self.width, KScaleWidth(8.0))];
    lineView.backgroundColor = kColorHEX(0xF0F0F0);
    [headView addSubview:lineView];
    
    [headView addSubview:self.titleTextField];
    _titleTextField.y = lineView.maxY;
    [headView addSubview:self.contentTextView];
    _contentTextView.y = _titleTextField.maxY + KScaleWidth(5.0);
    [_contentTextView addSubview:self.placeholderLabel];
    
    [headView addSubview:self.imgBgView];
    _imgBgView.y = _contentTextView.maxY + KScaleWidth(10.0);
    [_imgBgView addSubview:self.addImgBtn];
    
    //话题
    UIButton *topicBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(12.0) frame:CGRectMake(KScaleWidth(18.0), _imgBgView.maxY + KScaleWidth(16.0), KScaleWidth(54.0), KScaleWidth(27.0)) title:@"#话题" titleColor:kColorHEX(0x333333)];
    topicBtn.backgroundColor = kColorHEX(0xF5F5F5);
    [topicBtn setCornerRadius:topicBtn.height/2];
    [topicBtn addTarget:self action:@selector(topicAddAction:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:topicBtn];
    [headView addSubview:self.topicBgView];
    _topicBgView.y = topicBtn.maxY;
    
    UIView *lineView2 = [[UIView alloc] initWithFrame:CGRectMake(0, _topicBgView.maxY, self.width, KScaleWidth(8.0))];
    lineView2.backgroundColor = kColorHEX(0xF0F0F0);
    [headView addSubview:lineView2];
    headView.height = lineView2.maxY;
    
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _tbl.width, KScaleWidth(43.0) + KScaleWidth(30.0) + KScaleWidth(10.0))];
    footView.backgroundColor = kColorHEX(0xF5F5F5);
    UIButton *requireBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(15.0) frame:CGRectMake(0, 0, KScaleWidth(128.0), KScaleWidth(43.0)) title:@"对他的要求" titleColor:kColorHEX(0x666666) image:@"pub_require_icon"];
    [requireBtn addTarget:self action:@selector(requireAction:) forControlEvents:UIControlEventTouchUpInside];
    [requireBtn layoutButtonWithEdgeInsetsStyle:CSButtonEdgeInsetsStyleLeft ImageTitleSpace:KScaleWidth(6.0)];
    [footView addSubview:requireBtn];
    [footView addSubview:self.requireSelectBgView];
    _requireSelectBgView.y = requireBtn.maxY;
    _tbl.tableFooterView = footView;
}

#pragma mark - 接口数据
//加载见面详情
- (void)setcontentwithMeetingModel:(XMMeetingDetailModel *)meetingModel {
    //标题、描述、位置、时间
    _titleTextField.text = meetingModel.title;
    _contentTextView.text = meetingModel.descriptionStr;
    _placeholderLabel.hidden = (meetingModel.descriptionStr.length > 0);
    _startTimeStr = meetingModel.startTimeStr;
    _endTimeStr = meetingModel.endTimeStr;
    _positionStr = [NSString stringWithFormat:@"%@%@%@", meetingModel.addrProvince, meetingModel.addrCity, meetingModel.addrDetail];
    [self.selectTopicArr addObjectsFromArray:[meetingModel.topics componentsSeparatedByString:@","]];
    [self setTopicInfo];   //话题
    if (meetingModel.imgs.length > 0) {
        [self.selectImgArr addObjectsFromArray:[meetingModel.imgs componentsSeparatedByString:@","]];
        [self setSelectImgInfo];
    }
    //价格
    _priceStr = [NSString stringWithFormat:@"¥%@", meetingModel.totalPrice];
    //见面类型
    for (UIButton *itemBtn in self.itemBtnArr) {
        if ((itemBtn.tag - 9000) == [meetingModel.serviceId integerValue]) {
            itemBtn.selected = YES;
        }
        else {
            itemBtn.selected = NO;
        }
    }
    //年龄
    NSString *age;
    if (meetingModel.minage.length > 0) {
        age = [NSString stringWithFormat:@"%@-%@", meetingModel.minage, meetingModel.maxage];
    }
    NSString *sexStr = @[@"不限", @"男", @"女"][[meetingModel.sex integerValue]];
    //要求
    [self setRequireSelectInfoWithSex:sexStr
                                  age:age
                               height:meetingModel.shenggao
                               weight:meetingModel.tizhong
                                 star:meetingModel.xinzhuo];
    [_tbl reloadData];
}

//见面类型
- (void)setContentWithServiceModelArr:(NSArray *)serviceModelArr {
    _serviceModelArr = serviceModelArr;
//    [self.selectImgArr addObject:@"https://cx-service-img.oss-cn-beijing.aliyuncs.com/member_user/avater/1749457952127_430.file"];
//    [self.selectImgArr addObject:@"https://cx-service-img.oss-cn-beijing.aliyuncs.com/member_user/avater/1749457957343_287.file"];
    [self creatUI];
}

//见面地址
- (void)setContentWithAddressModel:(XMAddressModel *)addressModel {
    [self.publishDic safe_setObject:[[addressModel.areaName componentsSeparatedByString:@" "] firstObject] forKey:@"addrProvince"];
    [self.publishDic safe_setObject:[[addressModel.areaName componentsSeparatedByString:@" "] objectAtIndex:1] forKey:@"addrCity"];
    [self.publishDic safe_setObject:addressModel.detailAddress forKey:@"addrDetail"];
    _positionStr = [NSString stringWithFormat:@"%@%@", addressModel.areaName, addressModel.detailAddress];
    [_tbl reloadData];
}

//图片上传成功
- (void)setSelectImg:(NSString *)selectImgStr {
//- (void)setSelectImg:(UIImage *)selectImg {
//    [self.selectImgArr addObject:selectImg];
    [self.selectImgArr addObject:selectImgStr];
    [self setSelectImgInfo];
}

//话题
- (void)setTopicInfo {
    for (UIView *subView in self.topicBgView.subviews) {
        [subView removeFromSuperview];
    }
    CGFloat frameX = KScaleWidth(16.0);
    for (int i = 0; i < self.selectTopicArr.count; i ++) {
        NSString *title = self.selectTopicArr[i];
        CGFloat width = [title getWidthWithFont:KScaleWidth(12.0)] + KScaleWidth(20.0);
        UIButton *topicBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(12.0) frame:CGRectMake(frameX, (_topicBgView.height - KScaleWidth(27.0))/2, width, KScaleWidth(27.0)) title:self.selectTopicArr[i] titleColor:kColorHEX(0x9B9B9B)];
        topicBtn.backgroundColor = kColorHEX(0xF7F7F7);
        [topicBtn setCornerRadius:topicBtn.height/2];
        [topicBtn addTarget:self action:@selector(topicEditAction:) forControlEvents:UIControlEventTouchUpInside];
        [_topicBgView addSubview:topicBtn];
        frameX += (width + KScaleWidth(8.0));
    }
    _topicBgView.contentSize = CGSizeMake(MAX(frameX, _topicBgView.width), _topicBgView.height);
}

//图片
- (void)setSelectImgInfo {
    for (UIView *subView in self.imgBgView.subviews) {
        [subView removeFromSuperview];
    }
    [_imgBgView addSubview:self.addImgBtn];
    CGFloat frameX = KScaleWidth(15.0);
    for (int i = 0; i < self.selectImgArr.count; i ++) {
        NSString *imgStr = _selectImgArr[i];
        UIImageView *selectImg = [[UIImageView alloc] initWithFrame:CGRectMake(frameX , 0, KScaleWidth(90.0), KScaleWidth(78.0))];
        [selectImg setCornerRadius:KScaleWidth(10.0)];
        selectImg.userInteractionEnabled = YES;
        [selectImg sd_setImageWithURL:[NSURL URLWithString:imgStr]];
//        selectImg.image = _selectImgArr[i];
        [self.imgBgView addSubview:selectImg];
        
        UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(selectImg.width - KScaleWidth(20.0), 0, KScaleWidth(20.0), KScaleWidth(17.0)) image:@"pub_delete_icon" selectImg:@"pub_delete_icon"];
        [deleteBtn setCornerRadius:KScaleWidth(10.0)];
        [deleteBtn addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside];
        deleteBtn.tag = 11000 + i;
        [selectImg addSubview:deleteBtn];
        frameX += (KScaleWidth(90.0) + KScaleWidth(8.0));
    }
    XMWeakSelf;
    [UIView animateWithDuration:.3 animations:^{
        weakSelf.addImgBtn.x = frameX;
    }];
//    weakSelf.addImgBtn.x = frameX;
    frameX = frameX + _addImgBtn.width + KScaleWidth(8.0) + KScaleWidth(15.0);
    _imgBgView.contentSize = CGSizeMake(MAX(frameX, _imgBgView.width), _imgBgView.height);
}

- (void)saveSuccess {
    _positionStr = @"";
    _startTimeStr = @"";
    _endTimeStr = @"";
    _priceStr = @"";
    [self.selectImgArr removeAllObjects];
    [self.selectTopicArr removeAllObjects];
    for (UIView *subView in self.imgBgView.subviews) {
        [subView removeFromSuperview];
    }
    for (UIView *subView in self.topicBgView.subviews) {
        [subView removeFromSuperview];
    }
    for (UIView *subView in self.requireSelectBgView.subviews) {
        [subView removeFromSuperview];
    }
    self.addImgBtn.x = KScaleWidth(15.0);
    [_imgBgView addSubview:self.addImgBtn];
    [_tbl reloadData];
}

#pragma mark - BtnAction
- (void)backAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(backAction)]) {
        XMWeakSelf;
        [UIView animateWithDuration:.3 animations:^{
            weakSelf.y = self.height;
        } completion:^(BOOL finished) {
            [weakSelf removeFromSuperview];
        }];
        [self.delegate backAction];
    }
}

- (BOOL)canPublish {
    NSString *errStr = @"";
    if (!_titleTextField.text || _titleTextField.text.length == 0) {
        errStr = @"请输入标题";
    }
    
    else if (_startTimeStr.length == 0) {
        errStr = @"请选择开始时间";
    }
    else if (_endTimeStr.length == 0) {
        errStr = @"请选择结束时间";
    }
    else if (_priceStr.length == 0) {
        errStr = @"请输入价格";
    }
    if (errStr.length > 0) {
        [MBProgressHUD showError:errStr];
        return NO;
    }
    for (UIButton *itemBtn in self.itemBtnArr) {
        if (itemBtn.selected) {
//            for (XMServiceModel *serviceModel in self.serviceModelArr) {
//                if ([serviceModel.objectId integerValue] == itemBtn.tag - 9000) {
//                    [self.publishDic safe_setObject:serviceModel.objectId forKey:@"serviceId"];
//                }
//            }
            [self.publishDic safe_setObject:@(itemBtn.tag - 9000) forKey:@"serviceId"];
        }
    }
    NSString *starTime = [_startTimeStr stringByReplacingOccurrencesOfString:@"." withString:@"-"];
    NSString *endTime = [_endTimeStr stringByReplacingOccurrencesOfString:@"." withString:@"-"];
    [self.publishDic safe_setObject:_titleTextField.text forKey:@"title"];
    if (_contentTextView.text.length > 0) {
        [self.publishDic safe_setObject:_contentTextView.text forKey:@"description"];
    }
    [self.publishDic safe_setObject:starTime forKey:@"startTimeStr"];
    [self.publishDic safe_setObject:endTime forKey:@"endTimeStr"];
    [self.publishDic safe_setObject:[_priceStr stringByReplacingOccurrencesOfString:@"¥" withString:@""] forKey:@"totalPrice"];
    NSString *topicStr = [self.selectTopicArr componentsJoinedByString:@","];
    [self.publishDic safe_setObject:topicStr forKey:@"topics"];
    XMUserModel *userModel = [[XMUserManager shareInstance] getUser];
    [self.publishDic safe_setObject:userModel.userId forKey:@"userId"];
    if (self.selectImgArr.count > 0) {
        [self.publishDic safe_setObject:[self.selectImgArr firstObject] forKey:@"coverImageUrl"];
        [self.publishDic safe_setObject:[self.selectImgArr componentsJoinedByString:@","] forKey:@"imgs"];
    }
    return YES;
}

//发布
- (void)publishAction:(UIButton *)button {
    if ([self canPublish]) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(publishWithParameters:)]) {
            [self.publishDic safe_setObject:@"10" forKey:@"status"];
            [self.delegate publishWithParameters:self.publishDic];
        }
    }
}

//草稿
- (void)draftAction:(UIButton *)button {
    if ([self canPublish]) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(publishWithParameters:)]) {
            [self.publishDic safe_setObject:@"1" forKey:@"status"];
            [self.delegate publishWithParameters:self.publishDic];
        }
    }
}

//话题编辑
- (void)topicEditAction:(UIButton *)button {
    [self addSubview:self.topicView];
    [self.topicView showWithTopic:button.titleLabel.text];
    XMWeakSelf;
    _topicView.topicSelectBlock = ^(NSString * _Nonnull topicStr) {
        if (topicStr.length > 0) {
            [weakSelf.selectTopicArr removeObject:button.titleLabel.text];
            [weakSelf.selectTopicArr addObject:topicStr];
            [button setTitle:topicStr forState:UIControlStateNormal];
        }
        else {
            [weakSelf.selectTopicArr removeObject:button.titleLabel.text];
            [weakSelf.selectTopicArr removeObject:@""];
            [button removeFromSuperview];
            [weakSelf setTopicInfo];
        }
    };
}

//添加图片
- (void)addImgAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(upLoadImg)]) {
        [self.delegate upLoadImg];
    }
}

//对他的要求
- (void)requireAction:(UIButton *)button {
    [self addSubview:self.requireView];
    [_requireView show];
}

//位置选择
- (void)positionSelectAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(positionSelectAction)]) {
        [self.delegate positionSelectAction];
    }
}

//时间选择
- (void)dateSelectWithType:(NSString *)dateType {
    [self addSubview:self.dateSelectView];
    [self.dateSelectView show];
    XMWeakSelf;
    _dateSelectView.dateSelectBlock = ^(NSString * _Nonnull selectDateStr) {
        if ([dateType isEqualToString:@"1"]) {
            weakSelf.startTimeStr = selectDateStr;
        }
        else {
            weakSelf.endTimeStr = selectDateStr;
        }
        [weakSelf.tbl reloadData];
    };
}

//添加话题
- (void)topicAddAction:(UIButton *)button {
    [self addSubview:self.topicView];
    [_topicView showWithTopic:@""];
    XMWeakSelf;
    _topicView.topicSelectBlock = ^(NSString * _Nonnull topicStr) {
        if (topicStr.length > 0 && ![topicStr isEqualToString:@"#"]) {
            [weakSelf.selectTopicArr addObject:topicStr];
            [weakSelf setTopicInfo];
        }
    };
}

//见面类型选择
- (void)itemSelectAction:(UIButton *)button {
    for (UIButton *itemBtn in self.itemBtnArr) {
        itemBtn.selected = NO;
        itemBtn.borderColor = kColorHEX(0xF6F7F9);
        if (button.tag != itemBtn.tag) {
            for (XMServiceModel *serviceModel in self.serviceModelArr) {
                if ([serviceModel.objectId integerValue] == itemBtn.tag - 9000) {
                    UIImageView *img = [itemBtn viewWithTag:32];
                    [img sd_setImageWithURL:[NSURL URLWithString:serviceModel.icon]];
                }
            }
        }
        else {
            for (XMServiceModel *serviceModel in self.serviceModelArr) {
                if ([serviceModel.objectId integerValue] == itemBtn.tag - 9000) {
                    UIImageView *img = [itemBtn viewWithTag:32];
                    [img sd_setImageWithURL:[NSURL URLWithString:serviceModel.activeIcon]];
                }
            }
        }
    }
    button.selected = YES;
    button.borderColor = kColorHEX(0xFFD800);
}

//要求
- (void)setRequireSelectInfoWithSex:(NSString *)sexStr
                                age:(NSString *)ageStr
                             height:(NSString *)heightStr
                             weight:(NSString *)weightStr
                               star:(NSString *)starStr {
    for (UIView *subView in self.requireSelectBgView.subviews) {
        [subView removeFromSuperview];
    }
    CGFloat frameX = KScaleWidth(10.0);
    CGFloat space = KScaleWidth(10.0);
    if (![sexStr isEqualToString:@"不限"]) {
        UILabel *sexLabel = [[UILabel alloc] initWithFrame:CGRectMake(frameX, 0, KScaleWidth(60.0), KScaleWidth(28.0)) font:KFontSize(14.0) textColor:kColorHEX(0xF9439F) textAlignment:NSTextAlignmentCenter];
        sexLabel.backgroundColor = kColorHEX(0xFBF1F9);
        [sexLabel setRadius:KScaleWidth(3.0) borderColor:kColorHEX(0xFF8EC0) borderWidth:1];
        sexLabel.text = sexStr;
        [self.requireSelectBgView addSubview:sexLabel];
        frameX = sexLabel.maxX + space;
        if ([sexStr isEqualToString:@"男"]) {
            [self.publishDic safe_setObject:@"1" forKey:@"sex"];
        }
        else {
            [self.publishDic safe_setObject:@"2" forKey:@"sex"];
        }
    }
    else {
        [self.publishDic safe_setObject:@"0" forKey:@"sex"];
    }
    if (ageStr && ageStr.length > 0) {
        [self.publishDic safe_setObject:[[ageStr componentsSeparatedByString:@"-"] firstObject] forKey:@"minage"];
        [self.publishDic safe_setObject:[[ageStr componentsSeparatedByString:@"-"] lastObject] forKey:@"maxage"];
        NSString *ageTitle = [NSString stringWithFormat:@"%@岁", ageStr];
        CGFloat ageWidth = [ageTitle getWidthWithFont:KScaleWidth(14.0)] + KScaleWidth(20.0);
        UILabel *ageLabel = [[UILabel alloc] initWithFrame:CGRectMake(frameX, 0, ageWidth, KScaleWidth(27.0)) font:KFontSize(14.0) textColor:kColorHEX(0x436AF9) textAlignment:NSTextAlignmentCenter];
        ageLabel.backgroundColor = kColorHEX(0xF1F4FB);
        [ageLabel setRadius:KScaleWidth(3.0) borderColor:kColorHEX(0x8EBAFF) borderWidth:1];
        ageLabel.text = ageTitle;
        [self.requireSelectBgView addSubview:ageLabel];
        frameX = ageLabel.maxX + space;
    }
    if (heightStr && heightStr.length > 0) {
        [self.publishDic safe_setObject:heightStr forKey:@"shenggao"];
        NSArray *heightStrArr = [heightStr componentsSeparatedByString:@","];
        for (int i = 0; i < heightStrArr.count; i ++) {
            NSString *heightTitle = [NSString stringWithFormat:@"%@cm", heightStrArr[i]];
            CGFloat heightWidth = [heightTitle getWidthWithFont:KScaleWidth(14.0)] + KScaleWidth(20.0);
            UILabel *heightLabel = [[UILabel alloc] initWithFrame:CGRectMake(frameX, 0, heightWidth, KScaleWidth(27.0)) font:KFontSize(14.0) textColor:kColorHEX(0x28DCA9) textAlignment:NSTextAlignmentCenter];
            heightLabel.backgroundColor = kColorHEX(0xF1FBF5);
            [heightLabel setRadius:KScaleWidth(3.0) borderColor:kColorHEX(0x6FFA83) borderWidth:1];
            heightLabel.text = heightTitle;
            [self.requireSelectBgView addSubview:heightLabel];
            frameX = heightLabel.maxX + space;
        }
    }
    if (weightStr && weightStr.length > 0) {
        [self.publishDic safe_setObject:weightStr forKey:@"tizhong"];
        NSArray *weightStrArr = [weightStr componentsSeparatedByString:@","];
        for (int i = 0; i < weightStrArr.count; i ++) {
            NSString *weightTitle = [NSString stringWithFormat:@"%@kg", weightStrArr[i]];
            CGFloat weightWidth = [weightTitle getWidthWithFont:KScaleWidth(14.0)] + KScaleWidth(20.0);
            UILabel *weightLabel = [[UILabel alloc] initWithFrame:CGRectMake(frameX, 0, weightWidth, KScaleWidth(27.0)) font:KFontSize(14.0) textColor:kColorHEX(0x07D5ED) textAlignment:NSTextAlignmentCenter];
            weightLabel.backgroundColor = kColorHEX(0xF1FBFB);
            [weightLabel setRadius:KScaleWidth(3.0) borderColor:kColorHEX(0x8EEEFF) borderWidth:1];
            weightLabel.text = weightTitle;
            [self.requireSelectBgView addSubview:weightLabel];
            frameX = weightLabel.maxX + space;
        }
    }
    if (starStr && starStr.length > 0) {
        [self.publishDic safe_setObject:starStr forKey:@"xinzhuo"];
        NSArray *starStrArr = [starStr componentsSeparatedByString:@","];
        for (int i = 0; i < starStrArr.count; i ++) {
            NSString *starTitle = [NSString stringWithFormat:@"%@", starStrArr[i]];
            CGFloat starWidth = [starTitle getWidthWithFont:KScaleWidth(14.0)] + KScaleWidth(20.0);
            UILabel *starLabel = [[UILabel alloc] initWithFrame:CGRectMake(frameX, 0, starWidth, KScaleWidth(27.0)) font:KFontSize(14.0) textColor:kColorHEX(0xDC6428) textAlignment:NSTextAlignmentCenter];
            starLabel.backgroundColor = kColorHEX(0xFBF6F1);
            [starLabel setRadius:KScaleWidth(3.0) borderColor:kColorHEX(0xFAA56F) borderWidth:1];
            starLabel.text = starTitle;
            [self.requireSelectBgView addSubview:starLabel];
            frameX = starLabel.maxX + space;
        }
    }
    _requireSelectBgView.contentSize = CGSizeMake(MAX(self.width, frameX), _requireSelectBgView.height);
}

//删除图片
- (void)deleteAction:(UIButton *)button {
    [self.selectImgArr removeObjectAtIndex:button.tag - 11000];
    [self setSelectImgInfo];
}

- (void)show {
    XMWeakSelf;
    [UIView animateWithDuration:.3 animations:^{
        weakSelf.y = 0;
    }];
}

- (void)textViewDidChange:(UITextView *)textView {
    self.placeholderLabel.hidden = !(textView.text.length == 0);
}

#pragma mark - Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return KScaleWidth(44.0);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellName = @"XMPublishCellName";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellName];
        cell.textLabel.font = KFontSize(15.0);
        cell.textLabel.textColor = kColorHEX(0x333333);
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.detailTextLabel.font = KFontSize(15.0);
    }
    cell.textLabel.text = @[@"位置", @"开始时间", @"结束时间", @"价格"][indexPath.row];
    cell.imageView.image = ImageNamed((@[@"pub_position_icon", @"pub_start_time_icon", @"pub_end_time_icon", @"pub_price_icon"][indexPath.row]));
    cell.detailTextLabel.text = @[_positionStr, _startTimeStr, _endTimeStr, _priceStr][indexPath.row];
    if (indexPath.row == 3) {
        cell.detailTextLabel.font = KBoldFontSize(13.0);
        cell.detailTextLabel.textColor = kColorHEX(0xDD4B39);
    }
    else {
        cell.detailTextLabel.font = KFontSize(13.0);
        cell.detailTextLabel.textColor = kColorHEX(0x666666);
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row) {
        case 0: {
            if (self.delegate && [self.delegate respondsToSelector:@selector(positionSelectAction)]) {
                [self.delegate positionSelectAction];
            }
        }
            break;
        case 1: {
            [self dateSelectWithType:@"1"]; //开始时间
        }
            break;
        case 2: {
            [self dateSelectWithType:@"2"];  //结束时间
        }
            break;
        case 3: {
            [self addSubview:self.priceView];
            [_priceView show];
            XMWeakSelf;
            _priceView.priceInputBlock = ^(NSString * _Nonnull priceStr) {
                weakSelf.priceStr = [NSString stringWithFormat:@"¥%@", priceStr];
                [tableView reloadData];
            };
        }
            break;
            
        default:
            break;
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self endEditing:YES];
    return YES;
}

#pragma mark - Init
//- (UIScrollView *)bodyScrollView {
//    if (!_bodyScrollView) {
//        _bodyScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
//    }
//    return _bodyScrollView;
//}

- (UITableView *)tbl {
    if (!_tbl) {
        _tbl = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavagationBarH, self.width, self.height - kNavagationBarH)];
        _tbl.delegate = self;
        _tbl.dataSource = self;
        _tbl.separatorInset = UIEdgeInsetsMake(0, KScaleWidth(12.0), 0, KScaleWidth(12.0));
        _tbl.separatorColor = kColorHEX(0xF1F1F1);
        _tbl.backgroundColor = kColorHEX(0xF5F5F5);
        if (@available(iOS 15.0, *)) {
            _tbl.sectionHeaderTopPadding = 0;
        } else {
            _tbl.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            // Fallback on earlier versions
        }
    }
    return _tbl;
}

- (UITextField *)titleTextField {
    if (!_titleTextField) {
        _titleTextField = [[UITextField alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), 0, self.width - KScaleWidth(16.0)*2, KScaleWidth(54.0))];
        _titleTextField.font = KFontSize(16.0);
        _titleTextField.textColor = kColorHEX(0x333333);
        _titleTextField.placeholder = @"填写标题会被更多人看到...";
        _titleTextField.returnKeyType = UIReturnKeyDone;
        _titleTextField.delegate = self;
    }
    return _titleTextField;
}

- (UITextView *)contentTextView {
    if (!_contentTextView) {
        _contentTextView = [[UITextView alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), 0, self.width - KScaleWidth(16.0)*2, KScaleWidth(90.0))];
        _contentTextView.font = KFontSize(13.0);
        _contentTextView.delegate = self;
        _contentTextView.returnKeyType = UIReturnKeyDone;
//        _contentTextView.backgroundColor = kColorHEX(0x00fffff);
    }
    return _contentTextView;
}

- (UILabel *)placeholderLabel {
    if (!_placeholderLabel) {
        _placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(3.0), 0, self.contentTextView.width, KScaleWidth(30.0)) font:KFontSize(13.0) textColor:kColorHEX(0x999999)];
        _placeholderLabel.text = @"告诉TA特别的你要做什么特别的事，好文案更容易见面哦～";
        _placeholderLabel.userInteractionEnabled = NO;
    }
    return _placeholderLabel;
}

- (UIScrollView *)imgBgView {
    if (!_imgBgView) {
        _imgBgView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(78.0))];
    }
    return _imgBgView;
}

- (UIButton *)addImgBtn {
    if (!_addImgBtn) {
        _addImgBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(10.0) frame:CGRectMake(KScaleWidth(15.0), 0, KScaleWidth(90.0), KScaleWidth(78.0)) title:@"添加优质\n首图更吸引人" titleColor:kColorHEX(0x999999)];
        _addImgBtn.titleLabel.numberOfLines = 0;
        _addImgBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        _addImgBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [_addImgBtn setTitleEdgeInsets:UIEdgeInsetsMake(KScaleWidth(10.0), 0, -KScaleWidth(10.0), 0)];
        [_addImgBtn setBackgroundImage:ImageNamed(@"pub_add_img_icon") forState:UIControlStateNormal];
        [_addImgBtn addTarget:self action:@selector(addImgAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addImgBtn;
}

- (UIScrollView *)topicBgView {
    if (!_topicBgView) {
        _topicBgView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(58.0))];
    }
    return _topicBgView;
}

- (XMTopicView *)topicView {
    if (!_topicView) {
        _topicView = [[XMTopicView alloc] initWithFrame:self.bounds];
    }
    return _topicView;
}

- (XMDateSelectView *)dateSelectView {
    if (!_dateSelectView) {
        _dateSelectView = [[XMDateSelectView alloc] initWithFrame:self.bounds];
        _dateSelectView.dateType = XMDateDateTimeType;
    }
    return _dateSelectView;
}

- (XMPubPriceView *)priceView {
    if (!_priceView) {
        _priceView = [[XMPubPriceView alloc] initWithFrame:self.bounds];
    }
    return _priceView;
}

- (XMRequireView *)requireView {
    if (!_requireView) {
        _requireView = [[XMRequireView alloc] initWithFrame:self.bounds];
        XMWeakSelf;
        _requireView.requireBlock = ^(NSString * _Nonnull sexStr,
                                      NSString * _Nonnull ageStr,
                                      NSString * _Nonnull heightStr,
                                      NSString * _Nonnull weightStr,
                                      NSString * _Nonnull starStr) {
            [weakSelf setRequireSelectInfoWithSex:sexStr
                                              age:ageStr
                                           height:heightStr
                                           weight:weightStr
                                             star:starStr];
        };
    }
    return _requireView;
}

- (UIScrollView *)requireSelectBgView {
    if (!_requireSelectBgView) {
        _requireSelectBgView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(28.0))];
    }
    return _requireSelectBgView;
}

- (NSMutableArray *)itemBtnArr {
    if (!_itemBtnArr) {
        _itemBtnArr = [NSMutableArray array];
    }
    return _itemBtnArr;
}

- (NSMutableArray *)selectTopicArr {
    if (!_selectTopicArr) {
        _selectTopicArr = [NSMutableArray array];
    }
    return _selectTopicArr;
}

- (NSMutableDictionary *)publishDic {
    if (!_publishDic) {
        _publishDic = [NSMutableDictionary dictionary];
    }
    return _publishDic;
}

- (NSMutableArray *)selectImgArr {
    if (!_selectImgArr) {
        _selectImgArr = [NSMutableArray array];
    }
    return _selectImgArr;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
